package com.tim.test.leetcode.exam;


import java.util.Arrays;
import java.util.Scanner;

public class HuaWei20230613 {

	public static void main(String[] args) {
//		mealNum();
//		maxDiff(getMaxDiffInput());
//		Integer[] ints = new Random().ints(10, -10, 10)
//				.boxed().toArray(Integer[]::new);
//		System.out.println(Arrays.toString(ints));
//		maxDiff(ints);
		splitToil(getSplitToilInput());
	}

	/*
	 * M>Pi[0]
	 * sum[Pi[0~i]]<M+(i-1)*min
	 * 贪心算法，取最小值
	 */
	public static void mealNum(){
		Scanner scanner = new Scanner(System.in);
		int inputTimes = 0;
		int N = 0;// [0, 1000]
		int M = 0;// [Pi[0], 1000]
		Integer[] Pi;// Pi [0, 100]
		// 处理输入
		while (scanner.hasNextLine()){
			inputTimes++;
			if(inputTimes==1){
				N = scanner.nextInt();
				continue;
			}else if(inputTimes==2){
				M = scanner.nextInt();
				continue;
			}else{
				String s = scanner.nextLine();
				if(s.length()==0){
					continue;
				}
				Pi = Arrays.stream(s.trim().split("\\s+"))
						.map(Integer::valueOf)
						.toArray(Integer[]::new);
			}
			// 输入校验
			if(N<0 || N>1000
					|| M<0 || M>1000
					|| Pi.length!=N){
				throw new RuntimeException();
			}
			for(int num : Pi){
				if(num<0 || num>100){
					throw new RuntimeException();
				}
			}
			// sum[Pi[0~i]]<=M+(i-1)*min
			// min>=(sum-M)/(i-1)
			// 贪心算法，取较大值
			int min = 0;
			double sum = 0;
			for (int i = 0; i < N; i++) {
				sum += Pi[i];
				int temp = (int) Math.ceil((sum-M)/(i));
				min = Math.max(min, temp);
			}
			System.out.println(min);
			return;
		}
	}

	public static Integer[] getMaxDiffInput() {
		Scanner scanner = new Scanner(System.in);
		int inputTimes = 0;
		int n = 0;// (1, 10000]
		Integer[] nums=null;// int类型
		// 处理输入
		while (scanner.hasNextLine()) {
			inputTimes++;
			if (inputTimes == 1) {
				n = scanner.nextInt();
				continue;
			} else {
				String s = scanner.nextLine();
				if (s.length() == 0) {
					continue;
				}
				nums = Arrays.stream(s.trim().split("\\s+"))
						.map(Integer::valueOf)
						.toArray(Integer[]::new);
			}
			// 输入校验
			if (n < 2 || n > 10000
					|| nums.length != n) {
				throw new RuntimeException();
			}
			break;
		}
		return nums;
	}

	// 数组总和：sum=sumA+sumB
	// max(abs(sumA-sumB))
	// 先求总和，再逐个计算差值最大，记录max、index
	public static void maxDiff(Integer[] nums){

		// 数组总和：sum=sumA+sumB
		// max(abs(sumA-sumB))；A、B均非空，遍历[0, n-2]
		// 先求总和，再逐个计算差值最大，记录max
		long sum = 0;// int可能越界
		long sumA = 0;// 左侧总和
		long max = Long.MIN_VALUE;
		for (int i = 0; i < nums.length; i++) {
			sum += nums[i];
		}
		// 1 -2 3 4 -9 7
		// 1 2 3 4
		for (int i = 0; i < nums.length-1; i++) {// 终止点为n-2
			sumA += nums[i];
			max = Math.max(max, Math.abs(sum-sumA*2));// B-A
		}
		System.out.println(max);
		return;
	}

	public static Integer[] getSplitToilInput() {
		Scanner scanner = new Scanner(System.in);
		int inputTimes = 0;
		int n = 0;// (1, 10000]
		Integer[] nums=null;// int类型
		// 处理输入
		while (scanner.hasNextLine()) {
			inputTimes++;
			if (inputTimes == 1) {
				n = scanner.nextInt();
				continue;
			} else {
				String s = scanner.nextLine();
				if (s.length() == 0) {
					continue;
				}
				nums = Arrays.stream(s.trim().split("\\s+"))
						.map(Integer::valueOf)
						.toArray(Integer[]::new);
			}
			// 输入校验
			if (n < 2 || n > 100
					|| nums.length != n) {
				throw new RuntimeException();
			}
			for(int num : nums){
				if(num<1 || num>1000000){
					throw new RuntimeException();
				}
			}
			break;
		}
		return nums;
	}

	/*
	 * 积木总重量：sum = sumS^sumK；
	 * 可以不哭，sumS==sumK；sum = sumS^sumK = 0
	 * sum = num[0]^nums[1]...^nums[i-1]
	 * 		= num[0] ^ (nums[1]...^nums[i-1])
	 * 		=0
	 * koko分得最小的数值
	 */
	public static void splitToil(Integer[] nums){
		int sum = nums[0] ^ nums[1];
		for (int i = 2; i < nums.length; i++) {
			sum ^= nums[i];
		}
		if(sum!=0){
			System.out.println(-1);
			return;
		}
		sum = 0;
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < nums.length; i++) {
			sum += nums[i];
			min = Math.min(min, nums[i]);
		}
		System.out.println(sum-min);
	}


}
